package com.longdaji.decoration.ui.activitiesOfMine.refund.refundDetail;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.bean.RefundGoods;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.TimeUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefundDetailActivity extends BaseActivity implements RefundDetailContract.IView {

    @Inject
    RefundDetailContract.IPresenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.tv_refund_state)
    TextView tvRefundState;
    @BindView(R.id.tv_refund_total_money)
    TextView tvRefundTotalMoney;
    @BindView(R.id.tv_refund_alipay)
    TextView tvRefundAlipay;
    @BindView(R.id.tv_refund_reason)
    TextView tvRefundReason;
    @BindView(R.id.tv_refund_money)
    TextView tvRefundMoney;
    @BindView(R.id.tv_application_time)
    TextView tvApplicationTime;
    @BindView(R.id.tv_refund_number)
    TextView tvRefundNumber;
    @BindView(R.id.tv_refund_time)
    TextView tvRefundTime;
    @BindView(R.id.iv_commodity_picture)
    ImageView ivCommidityPicture;
    @BindView(R.id.tv_commodity_name)
    TextView tvCommodityName;
    @BindView(R.id.tv_commodity_attr)
    TextView tvCommodityAttr;

    private String refundOrderNo;
    private int refundGoodsId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_detail);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("退款详情");

        initView();
    }

    private void initView() {
        // TODO: 2018/4/11 0011 根据传过来的orderNo(或orderId)获取当前订单信息，然后在成功的时候更新界面
        refundOrderNo = getIntent().getStringExtra(Constants.ORDER_NO);
        refundGoodsId = getIntent().getIntExtra(Constants.GOODS_ID, 0);
        mPresenter.initData(refundOrderNo, refundGoodsId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showRefundDetail(Orders.OrdersBean.OrderBean order) {
        // TODO: 2018/4/19 0019 这里应该改成退款状态
        tvRefundState.setText(order.getOrderStatus());
        // TODO: 2018/4/19 0019 支付宝退款数额暂时用总额
        tvRefundAlipay.setText("￥" + order.getOrderAmountTotal());
        tvRefundMoney.setText("退款金额￥" + order.getOrderAmountTotal());
        // TODO: 2018/4/19 0019 退款时间从前面获取，并写入到退款时间中
        tvApplicationTime.setText("test application time");
        tvRefundReason.setText("test reason");
        tvRefundNumber.setText(order.getOrderNo());
        tvRefundTotalMoney.setText("￥" + order.getOrderAmountTotal());
    }

//    @Override
//    public void showRefundProduct(RefundGoods.RefundProductBean refundProduct) {
//        LogUtil.i("refundtest", "showRefundProduct");
//        String refundStatus = "";
//        // TODO: 2018/5/23 0023 这里状态要问一下后台是不是这样
//        switch (refundProduct.getRefundStatus()) {
//            case "0":
//                refundStatus = "退款中";
//                break;
//            case "1":
//                refundStatus = "退款成功";
//                break;
//            default:
//                break;
//        }
//        tvRefundState.setText(refundStatus);
//        tvRefundAlipay.setText("￥" + refundProduct.getRefundMoney());
//        tvRefundTotalMoney.setText("￥" + refundProduct.getRefundMoney());
//        tvRefundReason.setText(refundProduct.getRefundReason());
//        tvRefundNumber.setText(refundProduct.getOrderNo());
//        tvRefundTime.setText(TimeUtil.getDateToString(refundProduct.getRefundDate()));
//        tvRefundMoney.setText("￥" + refundProduct.getRefundMoney());
//    }


    @Override
    public void showRefundProduct(RefundGoods refundGoods) {
        LogUtil.i("mytest", "showRefundProduct: " + refundGoods.toString());
        String refundStatus = "";
        // TODO: 2018/5/23 0023 这里状态要问一下后台是不是这样
        switch (refundGoods.getRefundStatus()) {
            case "0":
                refundStatus = "退款中";
                break;
            case "1":
                refundStatus = "退款成功";
                break;
            default:
                break;
        }
        tvRefundState.setText(refundStatus);
        tvRefundAlipay.setText("￥" + refundGoods.getRefundMoney());
        tvRefundTotalMoney.setText("￥" + refundGoods.getRefundMoney());
        tvRefundReason.setText("退款原因:" + refundGoods.getRefundReason());
        tvRefundNumber.setText("订单编号:" + refundGoods.getOrderNo());
        tvRefundTime.setText(TimeUtil.getDateToString(refundGoods.getRefundDate()));
        tvRefundMoney.setText("退款金额:￥" + refundGoods.getRefundMoney());
        Glide.with(this).load(refundGoods.getGoodsSkuImg()).into(ivCommidityPicture);
        tvCommodityName.setText(refundGoods.getGoodsName());
        tvCommodityAttr.setText(refundGoods.getGoodsAttr());
    }
}
